package demo2;

import java.awt.*;
import java.util.LinkedList;

public class MyLinkList {
    public static void main(String[] args) {
        LinkedList<Integer>list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list.size());
        System.out.println(list);
    }
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val){
            this.val=val;
        }
    }

    private ListNode head;  //标记双向链表头部
    private ListNode tail;  //标记双向链表尾部

    public void addFirst(int data){
        if (this.head == null){
            this.head = new ListNode(data);
            this.tail =this.head;
        }else {
            ListNode node = new ListNode(data);
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
    }

    //尾插
    public void addLast(int data){
        if(this.tail == null){
            this.tail =new ListNode(data);
            this.head = this.tail;
        }else {
            ListNode node = new ListNode(data);
            this.tail.next = node;
            node.prev = this.tail;
            this.tail = node;
        }
    }

    //任意位置插入，第一个数据节点为0下标
    public boolean addIndex(int index,int data){
        int size = 0;
        if (index <0 ||index >size){
            System.out.println("index的位置不合法！");
            return false;
        }

        if (index==0){
            addFirst(data);
        } else if (index == size) {
            addLast(data);
        }else {
            ListNode cur = head;
            while (index !=0){
                cur = cur.next;
                index--;
            }

            ListNode node = new ListNode(data);

            ListNode perv = cur.prev;

            cur.prev = node;
            node.next = cur;

            perv.next =node;
            node.prev =perv;
        }

        return true;
    }

    //查找是否包含关键字key是否在单键表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur !=null){
            if (cur.val ==key){
                return true;
            }
            cur=cur.next;
        }
        return  false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){

        //链表为空
        if (head == null){
            return ;
        } else if (head.val == key) {
            //要删除的元素在头
            head = head.next;
            if (head == null)
                return;
            head.prev = null;
        } else if (tail.val == key) {
            //要删除的元素在尾
            tail = tail.prev;
            tail.next = null;
        }else {
            ListNode cur = head;
            //注意考虑一个元素的情况
            while (cur.next != null && cur != tail){
                if (cur.val == key){
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                    return;
                }
                cur = cur.next;
            }
        }
    }
}
